<template>
	<div id="formLogin">
		<h3>用户登录</h3>
		<el-form ref="form" label-width="80px">
			<el-form-item label="用户名">
				<el-input v-model="loginData.userName"></el-input>
			</el-form-item>
			<el-form-item label="密码">
				<el-input v-model="loginData.password"></el-input>
			</el-form-item>
			<el-form-item>
			    <el-button type="primary" @click="indexLogin">登录</el-button>
			    <el-button @click="indexRegister">注册</el-button>
		</el-form-item>
	  </el-form>
	</div>
</template>
<script type="text/javascript">
	import { login } from '@/api/api.js'
	export default {
		data() {
			return {
				loginData: {
					userName: undefined,
					password: undefined
				}
			}
		},
		methods: {
			indexLogin() {
				// 调用接口 api/api.js里面的导出 export function XXX (){}
				login({
					username: this.loginData.userName,
					password: this.loginData.password
				}).then((res) => {
					console.log("res",res)
					if(res.data.code == 200) {
						console.log('登录成功')
						// 登录成功就跳转页面
						this.$router.push('/home')
					}else if(res.data.code == 100) {
						console.log('没有这个用户')
					}else if(res.data.code == 101) {
						console.log('密码不正确')
					}
				})
			},
			indexRegister() {
				this.$router.push('/register')
			}
		}
	}
</script>
<style type="text/css" scoped>
	.el-form-item  {
		width: 52%;
		margin: 0 auto;
		margin-top: 20px;
	}
	.el-form {
		width: 60%;
		margin: 0 auto;
		background-color: lavender;
		overflow: hidden;
		padding: 40px 0px;
	}
	#formLogin {
		margin-top: 100px;
	}
	h3 {
		text-align: center;
		background-color: lavender;
		width: 60%;
		margin: 0 auto;
	}
</style>